package com.vogue.family.ui.vm;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.tencent.mmkv.MMKV;
import com.vogue.component.retrofit.VogueRetrofit;
import com.vogue.family.data.entity.BaseResponse;
import com.vogue.family.data.api.NetWorkManager;
import com.vogue.family.data.entity.User;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
import org.jetbrains.annotations.NotNull;

public class SplashViewModel  extends AndroidViewModel {

    private MutableLiveData<String> errorMsg;
    private MutableLiveData<User> userInfo;

    private MMKV mmkv = MMKV.defaultMMKV();

    public SplashViewModel(@NonNull @NotNull Application application) {
        super(application);
        errorMsg=new MutableLiveData<>();
        userInfo=new MutableLiveData<>();

    }

    public MutableLiveData<String> getErrorMsg() {
        return errorMsg;
    }

    public MutableLiveData<User> getUserInfo() {
        return userInfo;
    }

    public void onCheckToken(){
        String accessToken = mmkv.decodeString("accessToken","");
        if (TextUtils.isEmpty(accessToken)){
            errorMsg.setValue("没有登录信息");
            return;
        }

        VogueRetrofit.getInstance().addHeader("accessToken",accessToken);

        NetWorkManager
                .getInstance()
                .getApiService()
                .checkToken()
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {

                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseResponse<User>>() {

                    @Override
                    public void onNext(BaseResponse<User> response) {
                        if (response.getCode()==0){
                            userInfo.setValue(response.getData());
                        }else {
                            errorMsg.setValue(""+response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        errorMsg.setValue("错误信息："+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void onLoadUserInfo(){

        String accessToken = mmkv.decodeString("accessToken","");
        if (TextUtils.isEmpty(accessToken)){
            errorMsg.setValue("没有登录信息");
            return;
        }

        VogueRetrofit.getInstance().addHeader("accessToken",accessToken);

        NetWorkManager
                .getInstance()
                .getApiService()
                .userInfo()
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {

                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseResponse<User>>() {

                    @Override
                    public void onNext(BaseResponse<User> response) {
                        if (response.getCode()==0){
                            userInfo.setValue(response.getData());
                        }else {
                            errorMsg.setValue(""+response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        errorMsg.setValue("错误信息："+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
